// components/files/index.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    previewFile(e) {
      const {
        src
      } = e.currentTarget.dataset
      console.log(src);
      wx.downloadFile({
        url: src,
        header: {},
        success: function (res) {
          if (res.statusCode === 200) {

            let filePath = res.tempFilePath;
            let fileType = filePath.substr(filePath.lastIndexOf('.') + 1, filePath.length)

            switch (fileType) {
              case 'msword':
                fileType = 'doc';
                break;
              case 'jpeg':
                fileType = 'jpg';
                break;
            }

            wx.openDocument({
              filePath: filePath,
              fileType,
              success: function (res) {},
              fail: function (res) {
                wx.navigateTo({
                  url: `/pages/webpage/webpage?url=${src}`,
                })

              },
              complete: function (res) {}
            })
          }

        },
        fail: function (res) {
          Toast('下载文件失败')
        },
        complete: function (res) {},
      })
    }
  }
})